<?php

namespace App\Controller\Pay;

use App\Base\BaseController4Restful;
use App\Entity\OrderCancel;
use App\Repository\MoneyPayOrderRepository;
use App\Repository\OrderCancelRepository;
use App\Service\PayService;
use Symfony\Component\Routing\Annotation\Route;



class CallBackController extends BaseController4Restful
{
    /**
     * @Route("/pay/cb/ali", name="AppPayCallBackAliPay")
     */
    public function AppPayCallBackAliPay(PayService $ps)
    {
        if(!$sn = $this->getParamRequired("out_trade_no")){$this->logger->info("whereismysn");die("whereismysn");}
        if(!$from = $this->getParamRequired("passback_params")){$this->logger->info("whereareufrom");die("whereareufrom");}
        switch ($from) {
            case "insure":
                $ps->dealWithPayCallBackInsure($sn);
                break;
            case "taskhire":
                $ps->dealWithCallBackTaskHire($sn);
                break;
            case "order":
                $ps->dealWithCallBackOrder($sn);
                break;
            case "charge":
                $ps->dealWithCallBackUserCharge($sn);
                break;
        }
        return $this->json_out(200,"OK");
    }
    /**
     * @Route("/pay/cb/wx", name="AppPayCallBackWeChat")
     */
    public function AppPayCallBackWeChat(PayService $ps)
    {
        $postStr  = file_get_contents('php://input');
        //禁止引用外部xml实体
        $bPreviousValue = libxml_disable_entity_loader(true);
        $array_data = json_decode(json_encode(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        libxml_disable_entity_loader($bPreviousValue);

        if(isset($array_data['return_code']) and  $array_data['return_code'] == 'SUCCESS' ){
            //todo 检验来源的合法性
//            file_put_contents("./../FileData/log_pay_cb.txt","array_data:".json_encode($array_data)."\n\n",FILE_APPEND);
            $sn = $array_data["out_trade_no"];//取出回调码
            $from = $array_data["attach"];//取出预留参数
            switch ($from) {
                case "insure":
                    $ps->dealWithPayCallBackInsure($sn);
                    break;
                case "taskhire":
                    $ps->dealWithCallBackTaskHire($sn);
                    break;
                case "orders":
                    $ps->dealWithCallBackOrder($sn);
                    break;
                case "charge":
                    $ps->dealWithCallBackUserCharge($sn);
                    break;
            }
//            return $this->json_out(200,"OK");
        }
        echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';die;
    }
    /**
     * @Route("/pay/rf/wx", name="AppRefundCallBackWeChat")
     */
    public function AppRefundCallBackWeChat(
        PayService $ps
        ,OrderCancelRepository $orderCancelRepository
        ,MoneyPayOrderRepository $moneyPayOrderRepository
    )
    {
        $postStr  = file_get_contents('php://input');
        //禁止引用外部xml实体
        $bPreviousValue = libxml_disable_entity_loader(true);
        $array_data = json_decode(json_encode(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        libxml_disable_entity_loader($bPreviousValue);

        if(isset($array_data['return_code']) and  $array_data['return_code'] == 'SUCCESS' ){
            //todo 检验来源的合法性
//            file_put_contents("./../FileData/log_pay_cb.txt","array_data:".json_encode($array_data)."\n\n",FILE_APPEND);
            $sn = $array_data["out_trade_no"];//取出回调码

            $payOrder = $moneyPayOrderRepository->findOneBy(array("sn"=>$sn));
            $orderId = $payOrder->getParam()["orderid"]??0;
            //更新订单状态
            $order = $orderCancelRepository->findOneBy(array("order.id"=>$orderId));
            //更新退款记录状态
            $list = $orderCancelRepository->findBy(array("orders"=>$order));
            foreach ($list as $row) {
                $row->setStatus(1);
                $this->documentManager->persist($row);
            }
            $this->documentManager->flush();



//            $from = $array_data["attach"];//取出预留参数
//            switch ($from) {
//                case "insure":
//                    $ps->dealWithPayCallBackInsure($sn);
//                    break;
//                case "taskhire":
//                    $ps->dealWithCallBackTaskHire($sn);
//                    break;
//                case "orders":
//                    $ps->dealWithCallBackOrder($sn);
//                    break;
//                case "charge":
//                    $ps->dealWithCallBackUserCharge($sn);
//                    break;
//            }
//            return $this->json_out(200,"OK");
        }
        echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';die;
    }
}
